﻿function searchProgram(query, exact) {
    if (query == 'Փնտրել Հաղորդում...') {
        alert("Nothing to search");
        return;
    }
    else {
        /*Sending request to controller and getting JSON data to construct popup page and show results*/
        query = query.substring(0, 60);
        var query_64 = Base64.encode(query);
        //Replace('-', '+').Replace('_', '/')
        query_64 = query_64.replace(/\+/g, "-").replace(/\//g,"_");
        var url = "/ShowResults/" + query_64 + "/" + exact;
        window.location = url;
    }
}

function searchNotExact () {
   if ($("#txtSearchProgram").val() != "") {
      searchProgram($("#txtSearchProgram").val(), 0);
   }
}

function bodyOnloadCallbacks() {
    //register all nessesary functions to be executed after body load
    scrollToHilighted();
}

function scrollToHilighted() {
    var offset = $(".Home-page_Prog_onShare").offset();
    $('html, body').animate({
    scrollTop: offset.top
    },1500, function () {
           $('.MainMaster-Page_Main_Div').animate({
           scrollTop: offset.top
           });
     });
}

window.onscroll = function (e) {
    var obj = document.getElementById("home_prog_day_chooser");
	var catObj = document.getElementById('catchooser');
    if (window.pageYOffset > 128) {
        var obj = document.getElementById("home_prog_day_chooser");
        obj.style.width = "580px";
        obj.style.height = "30px";
        obj.style.position = "fixed";
        obj.style.top = "0";
        obj.style.marginTop = "0";
        obj.style.marginLeft = "6px";
        obj.style.zIndex = "3";
        //obj.style.borderRadius = "0 0 7px 7px";
        obj.style.borderBottom = "3px solid #0C64C0";
        obj.style.height = "30px";
		catObj.style.marginTop = "48px";
    }
    else {
        // obj.removeAttribute('style') doesn't work on google chrome
        obj.style.width = "";
        obj.style.height = "";
        obj.style.position = "";
        obj.style.top = "";
        obj.style.marginTop = "";
        obj.style.marginLeft = "";
        obj.style.zIndex = "";
        //obj.style.borderRadius = "0 0 7px 7px";
        obj.style.borderBottom = "";
        obj.style.height = "";
		catObj.style.marginTop = "0";
    } 
};

function keepDayChooserToTop()
{
    

}

// Converting to base64
var Base64 = {

    // private property
    _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode: function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // private method for UTF-8 encoding
    _utf8_encode: function (string) {
        string = string.replace(/\r\n/g, "\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if ((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    }

}